package com.yiji.fintech.musfinance.common.strategy.enginerules;

import com.acooly.core.utils.mapper.BeanCopier;
import com.yiji.fintech.musfinance.common.domain.base.BaseDomain;
import com.yiji.fintech.musfinance.common.domain.channel.ChannelDomain;
import com.yiji.fintech.musfinance.common.domain.channel.InstChannelApiDomain;
import com.yiji.fintech.musfinance.common.enginerules.rules.*;
import com.yiji.fintech.musfinance.common.enums.TrustModelEnum;
import com.yiji.fintech.musfinance.common.strategy.base.AbstractRulesStrategy;
import com.yiji.fintech.musfinance.common.strategy.context.TransactionContext;
import com.yiji.fintech.musfinance.common.utils.BizAssert;
import com.yiji.fintech.musfinance.entity.BfjBankAccount;
import com.yiji.fintech.musfinance.entity.CooperationInstBank;
import com.yiji.fintech.musfinance.entity.InstChannelApi;
import com.yiji.fintech.musfinance.service.BfjBankAccountService;
import com.yiji.fintech.musfinance.service.CooperationInstBankService;
import com.yiji.fintech.musfinance.service.InstChannelApiService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 通用策略
 * Created by ouwen@yiji.com} on 2017/9/22.
 */
@Service
@Slf4j
public class RulesStrategy extends AbstractRulesStrategy {
    @Autowired
    protected QuotaRuleService quotaRuleService;
    @Autowired
    protected WeightRulesService weightRulesService;
    @Autowired
    protected PriorityRuleService priorityRuleService;
    @Autowired
    protected BizQuotaRuleService bizQuotaRuleService;
    @Autowired
    protected RateRuleService rateRuleService;
    @Autowired
    protected BaseFilter dayQuotaRuleService;
    @Autowired
    protected BfjBankAccountService bfjBankAccountService;
    @Autowired
    protected CooperationInstBankService cooperationInstBankService;
    @Autowired
    protected InstChannelApiService instChannelApiService;

    /**
     * 构建渠道
     * @param channelDomain
     * @param domain
     */
    @Override
    protected void buildchannel(ChannelDomain channelDomain, BaseDomain domain) {
        List<InstChannelApiDomain> instChannelApiDomains =  channelDomain.getInstChannelApis();
        InstChannelApiDomain instChannelApiDomain = instChannelApiDomains.get(0);
        TransactionContext context = new TransactionContext();
        BeanCopier.copy(domain,context,BeanCopier.CopyStrategy.IGNORE_NULL, BeanCopier.NoMatchingRule.IGNORE,null);
        context.setGatewayChannelApi(instChannelApiDomain.getGatewayChannelApi());
        if(TrustModelEnum.TRUST == domain.getTrustModel()){//托管模式(网关维护秘钥信息)，纯路由

        }else {//非托管
            InstChannelApi instChannelApi = instChannelApiService.unique(instChannelApiDomain.getGatewayChannelApi());
            BfjBankAccount bfjBankAccount = bfjBankAccountService.unique(String.valueOf(instChannelApi.getId()));
            if(bfjBankAccount!=null){
                context.setBfjBankAccountName(bfjBankAccount.getBankAccountName());
                context.setBfjBankAccountNo(bfjBankAccount.getBankAccountNo());
                context.setBfjBankId(bfjBankAccount.getBankId());
                CooperationInstBank cooperationInstBank = cooperationInstBankService.getBankByBankId(bfjBankAccount.getBankId());
                BizAssert.notNull(cooperationInstBank,"未查询到【"+bfjBankAccount.getBankId()+"】合作银行配置");
                context.setBfjBankName(cooperationInstBank.getCoInstBankName());
                context.setBranchBankCity(cooperationInstBank.getCity());
                context.setBranchBankProvince(cooperationInstBank.getProvince());
                context.setUnionBankNo(cooperationInstBank.getUnionBankNo());
            }else{
                log.info("GID【{}】外部流水号【{}】内部流水号【{}】渠道【{}】未找到备付金信息",domain.getGid(),domain.getMerchOrderNo(),domain.getBizOrderNo(),instChannelApi.getGatewayChannelApi());
            }
        }
        channelDomain.setContext(context);
    }
    /**
     * ChannelDomain转换Domain
     * @param channelDomain
     * @param domain
     */
    @Override
    protected void converter(ChannelDomain channelDomain, BaseDomain domain) {

    }

    /**
     * 通用规则，业务不同规则不同则覆盖此方法
     * @param domain
     */
    @Override
    protected void process(ChannelDomain domain) {
        //笔限
        quotaRuleService.doExcute(domain);
        //日限
        dayQuotaRuleService.doExcute(domain);
        //规则金额
        bizQuotaRuleService.doExcute(domain);
        //优先级
        priorityRuleService.doExcute(domain);
        //渠道费率
        rateRuleService.doExcute(domain);
        //权重
        weightRulesService.doExcute(domain);
    }
}
